package com.wang.jmonkey.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.wang.jmonkey.modules.system.mapper.SystemRoleMapper;
import com.wang.jmonkey.modules.system.model.entity.SystemRoleEntity;
import com.wang.jmonkey.modules.system.model.enums.SystemAclPrincipalTypeEnum;
import com.wang.jmonkey.modules.system.model.enums.SystemDaclPrincipalTypeEnum;
import com.wang.jmonkey.modules.system.model.query.SystemRoleQuery;
import com.wang.jmonkey.modules.system.service.SystemAclService;
import com.wang.jmonkey.modules.system.service.SystemDaclService;
import com.wang.jmonkey.modules.system.service.SystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 角色信息(SystemRole)表服务实现类
 *
 * @author HeJiawang
 * @since 2021-05-14 16:10:59
 */
@Service
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, SystemRoleEntity> implements SystemRoleService {

    @Autowired
    private SystemRoleMapper mapper;

    @Autowired
    private SystemAclService systemAclService;

    @Autowired
    private SystemDaclService systemDaclService;

    /**
     * 获取分页数据
     * @param query query
     * @return 角色分页数据
     */
    @Override
    public PageInfo<SystemRoleEntity> page(SystemRoleQuery query) {
        Page<SystemRoleEntity> list = mapper.page(query);
        return new PageInfo<>(list);
    }

    /**
     * 删除角色信息
     * @param id 角色id
     * @return true 删除成功
     */
    @Transactional
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id)
                && systemAclService.deleteByPrincipalId(id, SystemAclPrincipalTypeEnum.ROLE)
                && systemDaclService.deleteByPrincipalId(id, SystemDaclPrincipalTypeEnum.ROLE);
    }

    /**
     * 获取所有角色信息
     * @return 角色信息
     */
    @Override
    public List<SystemRoleEntity> listAll() {
        return mapper.listAll();
    }
}
